package com.lvt.design.pattern.module.stat.v4;

import java.util.List;
import java.util.Map;

/**
 * @author LT
 * @date 2023年02月22日 23:46
 */
public abstract class ScheduleReporter {

    protected MetricsStorage metricsStorage;
    protected Aggregator aggregator;
    protected StatViewer viewer;

    public ScheduleReporter(MetricsStorage metricsStorage, Aggregator aggregator, StatViewer viewer) {
        this.metricsStorage = metricsStorage;
        this.aggregator = aggregator;
        this.viewer = viewer;
    }

    protected void doStatAndReport(long startTimeInMills, long endTimeInMills) {
        long durationInMills = endTimeInMills - startTimeInMills;
        Map<String, List<RequestInfo>> requestInfos = metricsStorage.getRequestInfos(startTimeInMills, endTimeInMills);
        Map<String, RequestStat> stats = aggregator.aggregate(requestInfos, durationInMills);
        viewer.output(stats, startTimeInMills, endTimeInMills);
    }
}
